The (X, Y) location of the center of the circle, and the radius.
It would have been OK to answer that the
(X, Y) location of the upper left corner,
and the width
and height of the containing rectangle are needed.
But if Circle
objects are only going to be used for circles
(not ovals) the center and radius are better.
Below is the class definition using this decision,
with two constructors included.
The first constructor has no parameters. It initializes the variables in the object to default values of zero. It is often useful to have a constructor like this, especially for objects whose data is expected to change.
class Circle { // variables int x, y, radius; // constructors public Circle() { x = 0; y = 0; radius = 0; } public Circle( int x, int y, int radius ) { . . . . . } // methods }
The second constructor allows the user to specify a value for each
of the object's variables.
It is convenient to call the parameters x
, y
, and radius
,
but there is a slight problem.
To use the parameter x
to initialize the object's variable x
you might try:
public Circle( int x, int y, int radius ) { x = x; // wrong!! }
This does not do what you want.
It would copy the value in the parameter x
into itself; a do-nothing operation!
Instead, the statement should be:
this.x = x; ------- | | +--- the parameter x | +---- the object's x
The reserved word this
is used to indicate this object's variable.
Complete the constructor: